
// 提供访问数据库的接口

const WXAPP = getApp();
const db = wx.cloud.database();
const _ = db.command;

/** 澡堂状态 */
const NWPU = db.collection("NWPU");

/** 预约队列 */
const QUEUE = db.collection("NWPU-Queue");

/** 用户信息数据库 */
const USERINFO = db.collection("userInfo");

/** 订单预约历史记录 */
const ORDERHISTORY = db.collection("orderHistory");

let orderHistoryDataFormat = {
    _id: '使用自动的ID',
    description: "这是数据格式示例",
    datetime: db.serverDate(),   // 使用时间
    duration: -1,                // 使用时长，-1表示违约
    domitory: "星A",
    floor: 1
};

// console.log("orderHistoryDataFormat\n", orderHistoryDataFormat);


/** 队列数据库的数据格式 */
let queueDataFormat = {
    _id: "user's openid",
    description: "这是数据格式示例",
    domitory: WXAPP.globalData.domitoryInfo.name,
    floor: WXAPP.globalData.domitoryInfo.floor,
    requestTime: db.serverDate(),
    acceptCrossFloor: true
}

// console.log("queueDataFormat\n", queueDataFormat);


let userInfoDataFormat = {
    _id: "user's openid",
    description: "这是数据格式示例",
    chooseLocation: WXAPP.globalData.chooseLocation,
    domitoryInfo: WXAPP.globalData.domitoryInfo
}

// console.log("userInfoDataFormat", userInfoDataFormat);



/** 第一次初始化数据库 */
function createCollection() {
    let domintories = [];
    let allTrue = [];
    for (let i = 0; i < 10; ++i){
        allTrue[i] = true;
    }
    for (let i of "ABCDEFGH") {
        domintories.push("星" + i);
    }
    for (let i of "ABCDEFGH") {
        domintories.push("云" + i);
    }
    for (let dom of domintories) {
        NWPU.add({
            data: {
                _id: dom,
                1: allTrue,
                2: allTrue,
                3: allTrue,
                4: allTrue,
                5: allTrue,
                6: allTrue
            }
        }).then(res => {
            console.log("成功创建宿舍", res);
        }).catch(err => {
            console.error("插入宿舍", dom, "失败\n", err) 
        });
    }
}

// createCollection();